package com.movisol.messengerprofileservice;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;

import android.content.Context;

import com.movisol.messengerprofileservice.data.Friend;
import com.movisol.messengerprofileservice.data.Friends;
import com.movisol.messengerprofileservice.data.UserProfile;
import com.movisol.messengerprofileservice.events.DataLoadedEvent;
import com.movisol.messengerprofileservice.interfaces.IProfileServiceManager;
import com.movisol.messengerprofileservice.listeners.IFriendsDataLoadedListener;
import com.movisol.messengerprofileservice.listeners.IUserProfileDataLoadedListener;

public class ProfileServiceMock implements IProfileServiceManager{
	
	protected List<IUserProfileDataLoadedListener> userProfileDataLoadedListeners = new ArrayList<IUserProfileDataLoadedListener>();
	protected List<IFriendsDataLoadedListener> friendsDataLoadedListeners = new ArrayList<IFriendsDataLoadedListener>();
	protected List<IFriendsDataLoadedListener> friendsDataUpdatedListeners = new ArrayList<IFriendsDataLoadedListener>();
	
	
	public ProfileServiceMock(Context context){
		
	}
	
	/******************UserProfileDataLoaded*****************/
	public synchronized void addUserProfileDataLoadedListener(IUserProfileDataLoadedListener listener){
		
		userProfileDataLoadedListeners.add(listener);
	}
	
	
	public synchronized void removeUserProfileDataLoadedListener(IUserProfileDataLoadedListener listener){
		
		userProfileDataLoadedListeners.remove(listener);
	}
	
	
	/**
	 * Fires all the Events for all the listener subscribed
	 */
	public void fireUserProfileDataLoadedEvent(String error){
		
		DataLoadedEvent dle = null;
		dle = new DataLoadedEvent(this, error);
		Iterator<IUserProfileDataLoadedListener> it = userProfileDataLoadedListeners.listIterator();
		while (it.hasNext()){
			 it.next().userProfileDataLoaded(dle);
		}
	}
	/********************************************************/
	
	
	
	/********************FriendsDataLoaded******************/
	public synchronized void addFriendsDataLoadedListener(IFriendsDataLoadedListener listener){
		
		friendsDataLoadedListeners.add(listener);
	}
	
	
	public synchronized void removeFriendsDataLoadedListener(IFriendsDataLoadedListener listener){
		
		friendsDataLoadedListeners.remove(listener);
	}
	
	
	/**
	 * Fires all the Events for all the listener subscribed
	 */
	public void fireFriendsDataLoadedEvent(String error){
		
		DataLoadedEvent dle = null;
		dle = new DataLoadedEvent(this, error);
		Iterator<IFriendsDataLoadedListener> it = friendsDataLoadedListeners.listIterator();
		while (it.hasNext()){
			 it.next().friendsDataLoaded(dle);
		}
	}
	/********************************************************/
	
	
		
	/********************FriendsDataUpdated*****************/
	public synchronized void addFriendsDataUpdatedListener(IFriendsDataLoadedListener listener){
		
		friendsDataUpdatedListeners.add(listener);
	}
	
	
	public synchronized void removeFriendsDataUpdatedListener(IFriendsDataLoadedListener listener){
		
		friendsDataUpdatedListeners.remove(listener);
	}
	
	
	/**
	 * Fires all the Events for all the listener subscribed
	 */
	public void fireFriendsDataUpdatedEvent(String error){
		
		DataLoadedEvent dle = null;
		dle = new DataLoadedEvent(this, error);
		Iterator<IFriendsDataLoadedListener> it = friendsDataUpdatedListeners.listIterator();
		while (it.hasNext()){
			 it.next().friendsDataLoaded(dle);
		}
	}
	/********************************************************/
	

	@Override 
	public String saveUserProfile(UserProfile profileData) {
		
			
		return "<UserProfile xmlns:i='http://www.w3.org/2001/XMLSchema-instance'>"+
					"<Birthdate>1985-01-23T00:00:00</Birthdate>"+
					"<Email>antonio@movisol.com</Email>"+
					"<EmailVerified>true</EmailVerified>"+
					"<Firstname>Antonio</Firstname>"+
					"<Gender>2</Gender>"+
					"<Lastname>Santiago</Lastname>"+
					"<Nickname>antoniomovisol</Nickname>"+
					"<PictureUrl>http://library.movisol.com/p/5/8/9/58946fb1-9823-4cf3-9ef6-c858d0a46ade.png</PictureUrl>"+
					"<Signature>E2CFE8A61E160B8DDFC548E8E6F92FF9</Signature>"+
					"<UserId>12e39f45-100b-411d-b11c-41d7e9800654</UserId>"+
				"</UserProfile>";

	}

	@Override
	public String getUserFriends(String sku, String version, String languageId, String userId, String signature) {

		String string;
		Random r = new Random();
		String tmp;
		String random = "";
		
		
		string = "<Friends xmlns:i='http://www.w3.org/2001/XMLSchema-instance'>" +
					"<FriendsList>";
		for(int i = 10; i > 0; i--)
		{
			tmp = "";
			random = "";
			
			for(int j = 4; j > 0; j--)
				random += (char)(97 + Math.abs(r.nextInt()%26));
			
			tmp = "<Friend>" +
					"<FriendStatusId>3</FriendStatusId>" +
					"<UserProfile>" +
						"<Email>"+random+"@gmail.com</Email>"+
					"</UserProfile>" +
				"</Friend>";
			string +=tmp;
		}
		
		string += "</FriendsList>" +
				"</Friends>";
		
		
		return string;
//		"<Friends xmlns:i='http://www.w3.org/2001/XMLSchema-instance'>" +
//					"<FriendsList>" +
//						"<Friend>" +
//							"<FriendStatusId>3</FriendStatusId>" +
//							"<UserProfile>" +
//								"<Birthdate>2011-02-02T00:00:00</Birthdate>" +
//								"<CountryId>AR</CountryId" +
//								"><Email>noexiste@gmail.com</Email" +
//								"><Firstname>Prueba LC</Firstname>" +
//								"<Gender>2</Gender>" +
//								"<LanguageId>es</LanguageId>" +
//								"<Lastname>Leonardo C</Lastname>" +
//								"<Nickname>Prueba</Nickname>" +
//								"<PictureUrl/>" +
//								"<UserId>abdb876a-e610-669f-b3ac-8fadf9b93abf</UserId>" +
//							"</UserProfile>" +
//						"</Friend>" +
//						"<Friend>" +
//							"<FriendStatusId>2</FriendStatusId>" +
//							"<UserProfile>" +
//								"<Email>meaprueban@gmail.com</Email>" +
//								"<UserId/>" +
//							"</UserProfile>" +
//						"</Friend>" +
//						"<Friend>" +
//							"<FriendStatusId>1</FriendStatusId>" +
//							"<UserProfile>" +
//								"<Email>yoapruebo@gmail.com</Email>" +
//								"<UserId/>" +
//							"</UserProfile>" +
//						"</Friend>" +
//						"<Friend>" +
//							"<FriendStatusId>3</FriendStatusId>" +
//							"<UserProfile>" +
//								"<Birthdate>2011-02-02T00:00:00</Birthdate>" +
//								"<CountryId>ES</CountryId" +
//								"><Email>yo@gmail.com</Email" +
//								"><Firstname>Prueba</Firstname>" +
//								"<Gender>2</Gender>" +
//								"<LanguageId>es</LanguageId>" +
//								"<Lastname>Leonardo C</Lastname>" +
//								"<Nickname>Antonio</Nickname>" +
//								"<PictureUrl/>" +
//								"<UserId>abdb876a-e610-669f-b3ac-8fadf9b93abf</UserId>" +
//							"</UserProfile>" +
//						"</Friend>" +
//						"<Friend>" +
//							"<FriendStatusId>3</FriendStatusId>" +
//							"<UserProfile>" +
//								"<Birthdate>2011-02-02T00:00:00</Birthdate>" +
//								"<CountryId>ES</CountryId" +
//								"><Email>yo@gmail.com</Email" +
//								"><Firstname>Prueba</Firstname>" +
//								"<Gender>2</Gender>" +
//								"<LanguageId>es</LanguageId>" +
//								"<Lastname>Leonardo C</Lastname>" +
//								"<Nickname>El</Nickname>" +
//								"<PictureUrl/>" +
//								"<UserId>abdb876a-e610-669f-b3ac-8fadf9b93abf</UserId>" +
//							"</UserProfile>" +
//						"</Friend>" +
//						"<Friend>" +
//							"<FriendStatusId>3</FriendStatusId>" +
//							"<UserProfile>" +
//								"<Birthdate>2011-02-02T00:00:00</Birthdate>" +
//								"<CountryId>ES</CountryId" +
//								"><Email>yo@gmail.com</Email" +
//								"><Firstname>Prueba</Firstname>" +
//								"<Gender>2</Gender>" +
//								"<LanguageId>es</LanguageId>" +
//								"<Lastname>Leonardo C</Lastname>" +
//								"<Nickname>Ella</Nickname>" +
//								"<PictureUrl/>" +
//								"<UserId>abdb876a-e610-669f-b3ac-8fadf9b93abf</UserId>" +
//							"</UserProfile>" +
//						"</Friend>" +
//						"<Friend>" +
//							"<FriendStatusId>3</FriendStatusId>" +
//							"<UserProfile>" +
//								"<Birthdate>2011-02-02T00:00:00</Birthdate>" +
//								"<CountryId>ES</CountryId" +
//								"><Email>yo@gmail.com</Email" +
//								"><Firstname>Prueba</Firstname>" +
//								"<Gender>2</Gender>" +
//								"<LanguageId>es</LanguageId>" +
//								"<Lastname>Leonardo C</Lastname>" +
//								"<Nickname>Nosotros</Nickname>" +
//								"<PictureUrl/>" +
//								"<UserId>abdb876a-e610-669f-b3ac-8fadf9b93abf</UserId>" +
//							"</UserProfile>" +
//						"</Friend>" +
//						"<Friend>" +
//							"<FriendStatusId>3</FriendStatusId>" +
//							"<UserProfile>" +
//								"<Birthdate>2011-02-02T00:00:00</Birthdate>" +
//								"<CountryId>ES</CountryId" +
//								"><Email>yo@gmail.com</Email" +
//								"><Firstname>Prueba</Firstname>" +
//								"<Gender>2</Gender>" +
//								"<LanguageId>es</LanguageId>" +
//								"<Lastname>Leonardo C</Lastname>" +
//								"<Nickname>Vosotros</Nickname>" +
//								"<PictureUrl/>" +
//								"<UserId>abdb876a-e610-669f-b3ac-8fadf9b93abf</UserId>" +
//							"</UserProfile>" +
//						"</Friend>" +
//					"</FriendsList>" +
//				"</Friends>";
		
	}

	
	@Override
	public String updateUserFriends(String sku, String version, String languageId, String userId, Friends friends) {

		String result = "<Friends xmlns:i='http://www.w3.org/2001/XMLSchema-instance'>" +
							"<FriendsList>";
							 
		
		for(Friend friend : friends.getFriendList())
		{
			result +=	"<Friend>";
							
			if(friend.getFriendStatusId() != null)
			{
				result +="<FriendStatusId>"+ friend.getFriendStatusId()+"</FriendStatusId>";
			}
			else
			{
				result +="<FriendStatusId>2</FriendStatusId>";
			}
			result += "<UserProfile>" +
							"<Email>";
			
			result += friend.getUserProfile().getEmail()+"</Email>";
			
			result +=  		"</UserProfile>" +
						"</Friend>";
		}
		
		result +=		"</FriendsList>" +
					"</Friends>";
		
		return result;
	}

	
	@Override
	public String updateUserNotificacion(String sku, String version, String medium, String mediumId) {
		// TODO Auto-generated method stub
		
		return "";
	}

}
